<template>
  <div class="year-month-wrapper">
    <single-list-filter-without-cb :filter="filter"
                                   :type="'years'"
                                   :leftTipText="'年份'"
                                   @confirmSingleSelect="confirmSingleSelectYears"
                                   ref="yearTypeId"
                                   :pageChange="pageChange">
    </single-list-filter-without-cb>
    <single-list-filter-without-cb :filter="filter"
                                   :type="'month'"
                                   :leftTipText="'月份'"
                                   @confirmSingleSelect="confirmSingleSelectMonth"
                                   ref="monthTypeId"
                                   :pageChange="pageChange">
    </single-list-filter-without-cb>
  </div>
</template>

<script type="text/ecmascript-6">
  import SingleListFilterWithoutCb from '../last/filter/single-list-filter-without-checkbox.vue'
  import {
    getDefaultYear
  } from '../last/chart/utils/dataUtils'
  export default {
    components: {
      SingleListFilterWithoutCb
    },
    data() {
      return {}
    },
    props: {
      filter: {
        type: Object,
        default: null
      },
      pageChange: {
        type: Number,
        default: null,
      }
    },
    methods: {
      confirmSingleSelectMonth(filter){
        this.beginMonthTemp = filter < 10 ? '0' + filter : '' + filter
        this.$emit('getYearAndMonth', this.queryYear, this.beginMonthTemp, this.queryYear + this.beginMonthTemp)
      },
      confirmSingleSelectYears(filter) {
        this.queryYear = filter + ''
        // 获取当前年份和月份
        let myDate = new Date();
        let curYear = myDate.getFullYear();
        let curMonth = myDate.getMonth() + 1;
        if (this.queryYear == curYear) {
          this.setReportAndMonthDefault(curMonth - 2)
        } else {
          if (this.lastSelectYear == curYear) {
            this.setReportAndMonthDefault(0)
          }
        }
        // 设置上一个选择年份为
        this.lastSelectYear = this.queryYear
        this.$emit('getYearAndMonth', this.queryYear, this.beginMonthTemp, this.queryYear + this.beginMonthTemp)
//        this.getTownWorksBasicMedicalData();
      },
      setReportAndMonthDefault(position) {
        // 动态变换月份的值，例如当前时间是20180415，则可选择的月份为1,2,3,4
        this.$refs.yearTypeId.changeSingleListData('year', this.queryYear)
        this.$refs.monthTypeId.changeSingleListData('month', this.queryYear)
        // 当前月份等于position + 1
        if (position >= 0) {
          this.$refs.monthTypeId.setCurrentSelectPosition(position)
        } else {
          this.queryYear = this.queryYear - 1
          position = 11
          this.$refs.monthTypeId.setCurrentSelectPosition(position)
        }
        let startDateTem = position + 1
        this.beginMonthTemp = startDateTem < 10 ? '0' + startDateTem : '' + startDateTem
        this.endMonthTemp = this.beginMonthTemp
        this.$refs.monthTypeId.setIsVisible(true)
      },
      setDefaultMonthAndPosition(queryYear) {
        this.queryYear = queryYear
        setTimeout(() => {
          this.lastSelectYear = this.queryYear
          // 获取当前年份和月份
          let myDate = new Date();
          let curYear = myDate.getFullYear();
          let curMonth = myDate.getMonth() + 1;
          // 通过默认的年份选择项，动态变换月份的个数，例如2018，则月份为1,2,3,4
//            this.$refs.monthTypeId.changeSingleListData('month', this.queryYear)
          if (this.queryYear == curYear) {
            this.setReportAndMonthDefault(curMonth - 2)
          } else {
            this.setReportAndMonthDefault(0)
          }
          // 动态变换报告的类型
//            this.$refs.reportTypeId.changeSingleListData('reportType', this.queryYear)

          // 根据初始化的参数，加载数据
//          this.getTownWorksBasicMedicalData()
          this.$emit('getYearAndMonth', this.queryYear, this.beginMonthTemp, this.queryYear + this.beginMonthTemp)
        }, 500)
      }
    }
  };
</script>

<style scoped>
  .year-month-wrapper {
    display: inline-block;
  }

</style>
